package com.twohand.admin.Controller;

import com.twohand.admin.Annotation.Permission;
import com.twohand.admin.Dto.RoleListQueryDto;
import com.twohand.admin.Dto.RolePerssionDto;
import com.twohand.admin.Entity.Menu;
import com.twohand.admin.Entity.Role;
import com.twohand.admin.Service.MenuService;
import com.twohand.admin.Service.RoleMenuService;
import com.twohand.admin.Service.RoleService;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.RolePageListVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @Autowired
    RoleMenuService roleMenuService;
    @GetMapping("/all")
    List<Role> getAllRole(){
        return roleService.getRoleList();
    }
    @Permission(value = "role:list")
    @GetMapping
    RolePageListVo getRoleList(RoleListQueryDto roleListQueryDto){
        RolePageListVo l =  roleService.getRoleList(roleListQueryDto);
        return l;
    }

    @Permission(value = "role:edit")
    @PostMapping
    Boolean addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }

    @Permission(value = "role:edit")
    @PutMapping
    Boolean updateRole(@RequestBody Role role){
        return roleService.updateRole(role);
    }

    @Permission(value = "role:delete")
    @DeleteMapping("/{id}")
    Boolean removeRoleById(@PathVariable Integer id){
        return roleService.deleteRole(id);
    }

    @Permission(value = "permission:show")
    @GetMapping("/permission")
    List<Integer> rolePermission(Integer id){
        List<Integer> rolePermissions = roleService.getRoleMenu(id);
        return rolePermissions;
    }

    @Permission(value = "permission:save")
    @PostMapping("/permission")
    Boolean saveRoles(@RequestBody RolePerssionDto rolePerssionDto){
        //先删除当前角色所有
        roleMenuService.deleteByRoleId(rolePerssionDto.getRoleId());
        //插入新的
        return roleMenuService.batchInsertByMenuId(rolePerssionDto.getMenuIdList(),rolePerssionDto.getRoleId());
    }

}
